package com.hydra.baseui.imageview;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import androidx.annotation.Nullable;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by hydra on 2016/3/31.
 *
 */
public class AsyncImageView extends SimpleDraweeView implements OnGestureListener {

    private Uri mUri;

    private GestureDetector mGestureDetector;


	public AsyncImageView(Context context, GenericDraweeHierarchy hierarchy) {
		super(context, hierarchy);

        initAsyncImageView();
	}

	public AsyncImageView(Context context) {
		super(context);

        initAsyncImageView();
	}

	public AsyncImageView(Context context, AttributeSet attrs) {
		super(context, attrs);

        initAsyncImageView();
	}

	public AsyncImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

        initAsyncImageView();
	}

	public AsyncImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);

        initAsyncImageView();
	}

    private void initAsyncImageView() {
        mGestureDetector = new GestureDetector(getContext(), this);
    }

    /**
     * fresco 自己的 draweeView 把touch事件都拦截在了controller里了，不想重写conroller，直接重写view的touch
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    public void setImageUriStr(String uriStr) {
        setImageURI(uriStr == null ? null : Uri.parse(uriStr));
    }

    @Override
    public void setImageURI(Uri uri, Object callerContext) {
        mUri = uri;

        super.setImageURI(uri, callerContext);
    }

    @Nullable
    public String getImageURI() {
        return mUri == null ? null : mUri.toString();
    }

    /**
     * set resize for local file
     */
    public void setResizeImageUri(Uri uri, int width, int height) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(width, height))
                .build();

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(getController())
                .build();
        setController(controller);
    }

    @Override
    public boolean onDown(MotionEvent e) {
	    if(isClickable() || isLongClickable()) {
		    return true;
	    }
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return performClick();
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        performLongClick();
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }
}
